﻿// <copyright file="MenuItemRepository.cs" company="Clever Software">
// Copyright (c) 2013, 2014 All Right Reserved, http://www.cleversoftware.by
//
// This source is subject to the Microsoft Permissive License.
// Please see the License.txt file for more information.
// All other rights reserved.
//
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY 
// KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
//
// </copyright>
// <author>Clever Software</author>
// <email>info@cleversoftware.by</email>
// <date>2014-02-04</date> 
// <summary>CleverCMS source code</summary>

namespace CleverCMS.Cms.Data
{
    using System.Collections.Generic;
    using System.Linq;

    using CleverCMS.Cms.Data.Entities;

    public class MenuItemRepository : CmsGenericRepository<CmsMenuItem>, IMenuItemRepository
    {

        public MenuItemRepository(CmsDataContext ctx) : base(ctx)
        {
        }

        public IEnumerable<CmsMenuItem> LoadAllItems()
        {
            return base.LoadAll().Select(x => new CmsMenuItem() { Id = x.Id, Title = x.Title });
        }

        public CmsMenuItem LoadMenuItem(int id)
        {
            return base.Load(id);
        }

        public void SaveMenuItem(CmsMenuItem item)
        {
            context.Entry(item).State = System.Data.Entity.EntityState.Modified;
            base.Save(item, item.Id == 0);
        }

        public void Delete(int id)
        {
            base.Delete(id);
        }

        public int Count(int? id)
        {
           return base.dbSet.Where(x => x.MenuId == id).Count();
        }

        public void RenameMenuItem(int id, string newName) 
        {
            var item = base.dbSet.FirstOrDefault(x => x.Id == id);
            if (item != null)
            {
                item.Title = newName;
            }
            base.Save(item, false);
        }
    }
}